import QQMapWX from '@/utils/qqmap-wx-jssdk.js'
export const qqmapsdk = new QQMapWX({
	key: 'ZMOBZ-HSVKL-Q24PA-EINCW-4DFAO-4VBTW'
}) //地图实例e
// 将金纬度转换为地址
export function toAddress(latitude, longitude) {
	return new Promise((resolve, reject) => {
		qqmapsdk.reverseGeocoder({
			location: {
				latitude: latitude,
				longitude: longitude
			},
			success(response) {
				resolve({
					data: response.result,
					code: 1
				})
			}
		})
	})
}
// 获取搜索提示
export function sean(latitude, longitude, keyword, page) {
	return new Promise((resolve, reject) => {
		// 调用接口
		qqmapsdk.search({
			keyword: keyword, //搜索关键词
			page_index: page, //分页
			location: `${latitude},${longitude}`, //设置周边搜索中心点
			success: function(res) { //搜索成功后的回调
				var mks = []
				for (var i = 0; i < res.data.length; i++) {
					mks.push({ // 获取返回结果，放到mks数组中
						title: res.data[i].title,
						category: res.data[i].category,
						address: res.data[i].address,
						id: res.data[i].id,
						latitude: res.data[i].location.lat,
						longitude: res.data[i].location.lng,
						iconPath: "/resources/my_marker.png", //图标路径
						width: 20,
						height: 20
					})
				}
				resolve({
					code: 1,
					data: mks
				})
			},
			fail: function(res) {
				console.log(res);
			},
			complete: function(res) {
				console.log(res);
			}
		});
	})
}
// 路线规划
export function routePlanning(rise, beginning, mode) {
	return new Promise((resolve, reject) => {
		//调用距离计算接口
		qqmapsdk.direction({
			mode: 'driving', //可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
			//from参数不填默认当前地址
			from: rise,
			to: beginning,
			success: function(res) {
				var ret = res;
				var coors = ret.result.routes[0].polyline,
					pl = [];
				//坐标解压（返回的点串坐标，通过前向差分进行压缩）
				// console.log(coors)
				var kr = 1000000;
				for (var i = 2; i < coors.length; i++) {
					coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
				}
				//将解压后的坐标放入点串数组pl中
				for (var i = 0; i < coors.length; i += 2) {
					pl.push({
						latitude: coors[i],
						longitude: coors[i + 1]
					})
				}

				//此处为了能知道路段位置金纬度
				var steps = []
				ret.result.routes[0].steps.forEach(k => {
					k.position = {
						latitude: coors[k.polyline_idx[1] - 1],
						longitude: coors[k.polyline_idx[1]]
					}
					steps.push(k)
				})
				resolve({
					code: 1,
					polyline: pl,
					steps: steps
				})

			},
			fail: function(error) {
				console.error(error);
			},
			complete: function(res) {
				console.log(res);
			}
		});
	})
}
// 获取城市
export function obtainCity(rise, beginning, mode) {
	return new Promise((resolve, reject) => {
		//调用距离计算接口
		qqmapsdk.getCityList({
			success: function(res) { //成功后的回调
				console.log(res);
				resolve({
					code: 1,
					data: res.result[0]
				})
				// console.log('省份数据：', res.result[0]); //打印省份数据
				// console.log('城市数据：', res.result[1]); //打印城市数据
				// console.log('区县数据：', res.result[2]); //打印区县数据
			},
			fail: function(error) {
				console.error(error);
			},
			complete: function(res) {
				console.log(res);
			}
		});
	})
}